<template>
  <div class="contentCate">
    <el-card class="box-card" v-if="status === 'index'">
      <el-row>
        <el-input
          v-model="searchParams.mobile"
          placeholder="请输入手机号"
          clearable
          style="width:200px"
          size="mini"
        ></el-input>
        <el-input
          v-model="searchParams.userId"
          placeholder="请输入用户id"
          clearable
          style="width:200px"
          size="mini"
        ></el-input>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="getData"
          v-if="has('userPromotions/index')"
          >搜索</el-button
        >
        <el-button
          type="info"
          icon="el-icon-refresh"
          size="mini"
          @click="reset"
          v-if="has('userPromotions/index')"
          >重置</el-button
        >
      </el-row>
      <el-table
        :data="data.data"
        v-loading="table_loading"
        row-key="id"
        style="width: 100%;margin:10px auto"
        size="mini"
        :header-cell-style="{ background: '#f3f8fe', color: '#333' }"
      >
        <el-table-column
          fixed
          prop="id"
          label="ID"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="user_id"
          label="用户id"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="nickname"
          label="昵称"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="mobile"
          label="手机号"
          align="center"
        ></el-table-column>
        <el-table-column prop="status" label="状态" align="center">
          <template slot-scope="scope">
            <span v-if="scope.row.status == 1" style="color:red">审核中</span>
            <span v-if="scope.row.status == 2" style="color:green"
              >审核通过</span
            >
            <span v-if="scope.row.status == 3" style="color:red"
              >审核未通过</span
            >
          </template>
        </el-table-column>
        <el-table-column
          prop="created_at"
          label="提交时间"
          align="center"
        ></el-table-column>

        <el-table-column prop="status" label="操作" align="center">
          <template slot-scope="scope">
            <span v-if="scope.row.status == 2" style="color:green"
              >审核通过</span
            >
            <span v-if="scope.row.status == 3" style="color:red"
              >审核未通过</span
            >
            <el-button
              type="primary"
              size="mini"
              @click="update(scope.row.id, 2)"
              v-if="has('userPromotions/update') && scope.row.status == 1"
              >通过</el-button
            >
            <el-button
              size="mini"
              @click="update(scope.row.id, 3)"
              type="danger"
              v-if="has('userPromotions/update') && scope.row.status == 1"
              >拒绝</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        background
        style="text-align:center;margin:20px 0;"
        :current-page="searchParams.page"
        :page-sizes="pageSizes"
        layout="total,prev, pager, next,sizes"
        :total="data.total"
        @size-change="changeSize"
        @current-change="changePage"
      ></el-pagination>
    </el-card>
  </div>
</template>

<script>
import api from "../../api/api.js";
export default {
  name: "contentCate",
  data() {
    return {
      status: "index", // 页面控制
      data: [], // 表格诗句
      table_loading: true, // 表格加载中
      loading: false, //全局加载中
      pageSizes: [10, 20, 50, 100, 200, 500, 800, 1000, 2000], // 每页条数默认10
      searchParams: {
        page: 1,
        limit: 10,
        mobile: "",
        userId: ""
      },
      activeValue: 2,
      inactiveValue: 1
    };
  },
  created() {
    this.getData();
  },

  methods: {
    // 加载中
    load() {
      this.loading = this.$loading({
        lock: true,
        text: "数据加载中",
        spinner: "el-icon-loading",
        background: "rgba(0, 0, 0, 0.7)"
      });
    },
    // 取消加载
    unload() {
      this.loading.close();
    },
    update(id, status) {
      this.load();
      api.userPromotions
        .edit(id, { status: status })
        .then(res => {
          this.unload();
          if (res.data.code === 200) {
            this.$message.success("保存成功");
            this.getData();
          }
        })
        .catch(error => {
          console.log(error);
        });
    },
    // 获取所有数据
    getData() {
      this.table_loading = true;
      api.userPromotions
        .index({ params: this.searchParams })
        .then(res => {
          if (res.data.code == 200) {
            this.table_loading = false;
            this.data = res.data.data;
          }
        })
        .catch(error => {
          console.log(error);
        });
    },
    // 重置搜索参数重新搜索
    reset() {
      this.init();
      this.getData();
    },
    // 初始化
    init() {
      this.searchParams.page = 1;
      this.searchParams.mobile = "";
      this.searchParams.userId = "";
    },
    // 改变每页条数时
    changeSize(limit) {
      this.searchParams.limit = limit;
      this.getData();
    },
    // 改变当前页码时
    changePage(page) {
      this.searchParams.page = page;
      this.getData();
    }
  }
};
</script>

<style scoped lang="less">
.contentCate {
  margin: 0 auto;
  .search {
    margin: 0 auto 10px auto;
  }
}
</style>
